﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atom.Entity;
//using Atom.Data.ManagerObjects;
//using Atom.Data.BusinessObjects;
using Atom.Utility;
using Atom.DAL;
using System.Data;

namespace Atom.BLL
{
    public class C1BLL : BaseBLL
    {

        public List<C1Entity> GetC1Entity(DateTime dtBegin, DateTime dtEnd, string whereFilter)
        {
            List<C1Entity> listC1 = new List<C1Entity>();

            try
            {

                LeaveHosRecordDAL dal = new LeaveHosRecordDAL();



                List<AgeEntity> ageEntitys = MedInfo.GetAgeEntity();//获取年龄实体

                //C1Entity entity5Age = new C1Entity();
                //C1Entity entity18Age = new C1Entity();
                //C1Entity entity60Age = new C1Entity();
                //C1Entity entity80Age = new C1Entity();

                #region 保留

                //IHLeavehosRecordManager manager = ManagerFactory.GetHLeavehosRecordManager();

                ////获取时间段内的出院记录
                //IList<HLeavehosRecord> records = manager.GetByQuery(" from HLeavehosRecord where  Leavehosdate>='" + dtBegin.ToString("yyyy-MM-dd-00.00.00") + "' and Leavehosdate <='" + dtEnd.ToString("yyyy-MM-dd-00.00.00") + "'");

                //IHLeavehoschargeDetailManager hosCharge = ManagerFactory.GetHLeavehoschargeDetailManager();

                //ITPatientInfoManager paientInfo = ManagerFactory.GetTPatientInfoManager();

                ////获取时间段内的药品总费用

                //foreach (HLeavehosRecord item in records)
                //{


                //    //获取年龄
                //    TPatientInfo paient = paientInfo.GetById(item.Diagnoseid);
                //    if (paient != null)
                //    {
                //        if (paient.Age <= 5)
                //        {
                //            entity5Age.CHRQType = 0;
                //            entity5Age.RqType = "5岁及以下";


                //            entity5Age.InhosCount += 1;


                //            entity5Age.TotalFee += item.Totalconsumemoney;

                //            //获取药品费用
                //            IList<object> str = hosCharge.CreateSingleQuery("select count(Money) from HLeavehoschargeDetail where Inhosid ='" + item.Inhosid + "' and Currentinhosmark=" + item.Currentinhosmark + " and Cancelmark=0 and Itemid in ('01','02','03')");
                //            decimal ypFee = Convert.ToDecimal(str[0]);
                //            decimal zlFee = item.Totalconsumemoney - ypFee;
                //            entity5Age.YPFee += ypFee;


                //            if (item.Feetypeid == "新农合")
                //            {
                //                entity5Age.BCNMTotalFee += item.Totalconsumemoney;
                //                entity5Age.BCNMYPFee += ypFee;
                //                entity5Age.BCNMZLFee += zlFee;
                //            }

                //        }
                //        else if (paient.Age > 5 && paient.Age < 19)
                //        {
                //            entity18Age.CHRQType = 1;
                //            entity18Age.RqType = "6-18岁";

                //            entity18Age.TotalFee += item.Totalconsumemoney;
                //            entity18Age.InhosCount += 1;

                //            //获取药品费用
                //            IList<object> str = hosCharge.CreateSingleQuery("select count(Money) from HLeavehoschargeDetail where Inhosid ='" + item.Inhosid + "' and Currentinhosmark=" + item.Currentinhosmark + " and Cancelmark=0 and Itemid in ('01','02','03')");
                //            entity18Age.YPFee += Convert.ToDecimal(str[0]);

                //        }
                //        else if (paient.Age < 19 && paient.Age < 60)
                //        {
                //            entity60Age.CHRQType = 2;
                //            entity60Age.RqType = "19-59岁";

                //            entity60Age.TotalFee += item.Totalconsumemoney;
                //            entity60Age.InhosCount += 1;

                //            //获取药品费用
                //            IList<object> str = hosCharge.CreateSingleQuery("select count(Money) from HLeavehoschargeDetail where Inhosid ='" + item.Inhosid + "' and Currentinhosmark=" + item.Currentinhosmark + " and Cancelmark=0 and Itemid in ('01','02','03')");
                //            entity60Age.YPFee += Convert.ToDecimal(str[0]);
                //        }
                //        else
                //        {
                //            entity80Age.CHRQType = 3;
                //            entity80Age.RqType = "60岁及以上";

                //            entity80Age.TotalFee += item.Totalconsumemoney;
                //            entity80Age.InhosCount += 1;

                //            //获取药品费用
                //            IList<object> str = hosCharge.CreateSingleQuery("select count(Money) from HLeavehoschargeDetail where Inhosid ='" + item.Inhosid + "' and Currentinhosmark=" + item.Currentinhosmark + " and Cancelmark=0 and Itemid in ('01','02','03')");
                //            entity80Age.YPFee += Convert.ToDecimal(str[0]);

                //        }

                //    }

                //    // item.Inhosid
                //}

                #endregion

                //循环年龄段
                foreach (AgeEntity item in ageEntitys)
                {
                    DataSet dsTemp = new DataSet();
                    C1Entity entity = new C1Entity();
                    //设置年龄类别实体
                    entity.CHRQAgeType = item.Code;
                    entity.CHRQAgeName = item.Name;

                    //获取住院总费用及药品总费用
                    dsTemp = dal.GetC1EntityBasic(dtBegin, dtEnd, item.AgeBegin, item.AgeEnd, string.Empty, Constants.INVOICEITEM_YP);
                    entity.TotalFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["TOTALCONSUMEMONEY"]);
                    entity.YPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["INFACTMONEY"]);
                    //住院人次数 住院床总数
                    entity.InhosCount = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToInt32(dsTemp.Tables[0].Rows[0]["INHOSCOUNT"]);
                    entity.OuthosBedCount = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToInt32(dsTemp.Tables[0].Rows[0]["INHOSDAYS"]);

                    //获取补偿农民（新农合）住院总费用  药品费用
                    dsTemp = dal.GetC1EntityBasic(dtBegin, dtEnd, item.AgeBegin, item.AgeEnd, Constants.FEETYPE_NH, Constants.INVOICEITEM_YP);
                    entity.BCNMTotalFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["TOTALCONSUMEMONEY"]);
                    entity.BCNMYPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["INFACTMONEY"]);

                    //获取补偿农民（新农合）住院总费用  检查费用
                    dsTemp = dal.GetC1EntityBasic(dtBegin, dtEnd, item.AgeBegin, item.AgeEnd, Constants.FEETYPE_NH, Constants.INVOICEITEM_JC);
                    entity.BCNMZLFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["INFACTMONEY"]);


                    //获取补偿范围总费用，
                    dsTemp = dal.GetC1EntityTotalFee(dtBegin, dtEnd, item.AgeBegin, item.AgeEnd, Constants.FEETYPE_NH);
                    entity.BCFWTotalFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"]);

                    //获取补偿范围总费用，药品费用
                    dsTemp = dal.GetC1EntityDetail(dtBegin, dtEnd, item.AgeBegin, item.AgeEnd, Constants.FEETYPE_NH, Constants.SUMMARY_YP);
                    //entity.BCFWTotalFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["DISCOUNTPAY"]);
                    entity.BCFWYPFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["INFACTMONEY"]);

                    //获取补偿范围总费用，检查费用
                    dsTemp = dal.GetC1EntityDetail(dtBegin, dtEnd, item.AgeBegin, item.AgeEnd, Constants.FEETYPE_NH, Constants.SUMMARY_JC);
                    entity.BCFWZLFee = (dsTemp == null) ? 0 : (dsTemp.Tables[0].Rows.Count < 1) ? 0 : Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["INFACTMONEY"]);

                    listC1.Add(entity);

                }

                //listC1.Add(entity5Age);
                //listC1.Add(entity18Age);
                //listC1.Add(entity60Age);
                //listC1.Add(entity80Age);



            }
            catch (Exception ex)
            {

                log.Error("C1BLL-->"+ex.Message);
            }

            return listC1;
        }



    }
}
